package com.myacg.editor.common.unit.button;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import com.myacg.editor.common.utils.FontUtils;
import com.myacg.editor.common.utils.GradualChangeUtils;
import com.myacg.editor.common.utils.JFrameUtils;

/** 
* 只要改变getDelColors()中的算法就会出现不同渐变效果!!! 最简单的就是循序等额递减!! 
* 然后到处使用该Panel替代默认的JPanel,然后将上面的其他控件都设置成透明(setOpaque(false)),则整个系统就会显得很漂亮了! 
* 其他Swing控件都可以类似重构!!! 
* 但如果把其他控件都设成透明的，然后最后都加载在该面板上面,则其他控件都无需重构就会出现效果！！比如: 
* JTree tree= new JTree();  // 
* tree.setOpaque(false);  //透明! 
* JScrollPanel scroll = new JScrollPanel(tree);  // 
* scroll.setOpaque(false);  //透明 
* scroll.getViewPort().setOpaque(false); 
* TestBGPanel panel = new TestBGPanel(new BorderLayout());  //创建渐变效果面板 
* panel.add(scroll);  //将滚动框加载进来,则这个树的背景就是渐变效果了,则界面就丰富了!!! 
* @author xch 
* 
*/  
public class CrystalButton extends JLabel {  
  public static final String  STYLE_CRYSTAL = "Crystal";
  public static final String  STYLE_COLORDOWN =  "ColorDown"; 
  public static final String  STYLE_COLORUP =  "ColorUp";
  private Color  baseColor;
  private Color  borderColor;
  private String buttonName; 
  private Color  buttonNameColor;
  private double buttonNameSize;
  private int    colorPrefix;
  private double colorN;
  private double buttonNameSize_final;
  private int    colorPrefix_final;
  private double colorN_final;
  private String style;
  public CrystalButton(Color baseColor,String buttonName,int width , int height) {
	  initClickEvent();
	  this.baseColor = baseColor;
	  this.borderColor = Color.white;
	  this.buttonNameColor =Color.white;
	  //默认
	  this.buttonNameSize_final = 0.5;
	  this.colorPrefix_final = 57;
	  this.colorN_final = 0.8;
	  this.style = STYLE_COLORDOWN;
	  setButtonUI(buttonNameSize_final,colorPrefix_final, colorN_final);
	  this.buttonName = buttonName;
	  this.setFont(new Font("宋体", Font.PLAIN, 17));
	  //宽高
	  this.setPreferredSize(new Dimension(width, height));
  }
  
  
  /**
   * 初始化按钮各项参数
   * @param borderColor
   * @param buttonNameColor
   * @param buttonNameSize
   * @param colorPrefix
   * @param colorN
   */
  public void setButtonUI(double buttonNameSize,int colorPrefix,double colorN){
	  this.buttonNameSize = buttonNameSize;
	  this.colorPrefix  = colorPrefix;
	  this.colorN = colorN;
  }
  
  @Override  
  /** 
   * 关键是重构该方法!! 
   * 也可以重构JPanel的该方法 
   */  
  public void paint(Graphics g) {  
	  Graphics2D g2 = (Graphics2D) g; //  
	  int li_beginx = (int) g2.getClipBounds().getX(); //  
      int li_beginy = (int) g2.getClipBounds().getY(); //  
      g2.setColor(borderColor); //  
      g2.drawRect(li_beginx, li_beginy, this.getWidth() - 1, this.getHeight() - 1); //画边框  
      //关键计算,获取渐变每帧的样式
       this.setDelColorsByStyle (g2,this.style);
      //设置按钮文字
      g2.setColor(this.buttonNameColor); 
      //FontUtils.drawCenterString(buttonName,12, this, g2);
      FontUtils.drawCenterStringByPercentage(buttonName,this.buttonNameSize,this, g2);
  } 
  
  /**
   * 根据提供的样式对按钮进行渲染
   * @param style
   */
  private void setDelColorsByStyle(Graphics g2,String  style){
	  int[][] li_delcolors  = null ; 
    switch (style) {
	case STYLE_CRYSTAL:
		li_delcolors= GradualChangeUtils.getCrystalColors(this.getHeight() - 2 , this.colorPrefix,  this.colorN);
		break;
	case STYLE_COLORDOWN:
		li_delcolors= GradualChangeUtils.getChengeColors(this.getHeight() - 2 , this.colorPrefix,true);
		break;
	case STYLE_COLORUP:
		li_delcolors= GradualChangeUtils.getChengeColors(this.getHeight() - 2 , this.colorPrefix,false);
		break;
	}
	//未匹配上时  
	if(li_delcolors == null){
		GradualChangeUtils.getChengeColors(this.getHeight() - 2 , this.colorPrefix,true);  
	}
	//根据渐变色填充按钮
    GradualChangeUtils.drawColorsToPaenlUpStart(g2,this.baseColor, li_delcolors, this.getWidth()-2);
}
  
  
  private void initClickEvent(){
	  this.addMouseListener(new MouseAdapter() {
		//进入
		public void mouseEntered(MouseEvent e) {
			 setButtonUI(buttonNameSize_final,colorPrefix_final+10, colorN_final+0.1);
			 CrystalButton.this.updateUI();
		}
		//离开
		public void mouseExited(MouseEvent e) {
			 setButtonUI(buttonNameSize_final,colorPrefix_final, colorN_final);
			CrystalButton.this.updateUI();
		}
		//按下
		public void mousePressed(MouseEvent e) {
			 setButtonUI(buttonNameSize_final,colorPrefix_final-35, colorN_final+0.1);
			CrystalButton.this.updateUI();
		}
		//松开
		@Override
		public void mouseReleased(MouseEvent e) {
			 setButtonUI(buttonNameSize_final,colorPrefix_final+10, colorN_final-0.2);
			CrystalButton.this.updateUI();
		}
	  });
  }
 
  
  
public Color getBaseColor() {
	return baseColor;
}


public void setBaseColor(Color baseColor) {
	this.baseColor = baseColor;
}


public Color getBorderColor() {
	return borderColor;
}


public void setBorderColor(Color borderColor) {
	this.borderColor = borderColor;
}


public String getButtonName() {
	return buttonName;
}


public void setButtonName(String buttonName) {
	this.buttonName = buttonName;
}


public Color getButtonNameColor() {
	return buttonNameColor;
}


public void setButtonNameColor(Color buttonNameColor) {
	this.buttonNameColor = buttonNameColor;
}


public double getButtonNameSize() {
	return buttonNameSize_final;
}


public void setButtonNameSize(double buttonNameSize) {
	this.buttonNameSize = buttonNameSize;
	this.buttonNameSize_final = buttonNameSize;
}


public int getColorPrefix() {
	return colorPrefix_final;
}


public void setColorPrefix(int colorPrefix) {
	this.colorPrefix = colorPrefix;
	this.colorPrefix_final = colorPrefix;
}


public double getColorN() {
	return colorN_final;
}


public void setColorN(double colorN) {
	this.colorN = colorN;
	this.colorN_final = colorN;
}


public String getStyle() {
	return style;
}


public void setStyle(String style) {
	this.style = style;
}


//主方法  
  public static void main(String[] _args) {  
      JFrame frame = new JFrame("Java渲染水晶按钮"); 
      JFrameUtils.setDefaultWindow(frame, 500, 300);
      //流式布局
      frame.setLayout(new FlowLayout());
      //一批背景颜色!!  
	  Color[] colors = new Color[] {new Color(3, 26, 61), 
	    		  						new Color(76, 93, 142),
	    		  						new Color(144, 29, 67), 
	    		  						new Color(158, 84, 167)
	    		  					//	, new Color(120, 120, 120)
	  };  
	  
      for (int i = 0; i < colors.length; i++) { //根据背景颜色创建一系列按钮!!!  
          CrystalButton panel = new CrystalButton(colors[i],"测试按钮"+i,150 , 30); //  
          frame.getContentPane().add(panel); //  
      }  
      frame.setVisible(true); //  
  	}
  }  
